/**
 * 
 */
package com.mago.tis.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.mago.tis.bean.TradersBean;
import com.mago.tis.pojo.Tistit;
import com.mago.tis.service.ITraderService;
import com.mago.tis.serviceImpl.TraderService;
import com.mago.tis.utility.ApplicationConstant;
import com.mago.tis.utility.ApplicationException;
import com.mago.tis.utility.CollectionUtil;
import com.mago.tis.utility.ExceptionMessage;
import com.mago.tis.utility.StringUtil;

/**
 * @author Yogesh 
 * Add and delete Traders information
 *
 */
public class TradersController extends BaseController{
	private static final Logger logger = Logger
			.getLogger(LoginController.class);
	private ITraderService traderService=null;
	private String message = "";
	public TradersController(){
		traderService=new TraderService();
	}
	
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info("Tranport controller called");
	
	
		String action=request.getParameter(ApplicationConstant.ACTION);
		String companyId=request.getParameter(ApplicationConstant.COMP_ID);
		
		String name=request.getParameter(ApplicationConstant.TRADER_NAME);
		String address=request.getParameter(ApplicationConstant.TRADER_ADDRESS);
		String contactNo=request.getParameter(ApplicationConstant.TRADER_CONTACT_NO);
		
		String mobileNo=request.getParameter(ApplicationConstant.TRADER_MOBILE_NO);
		String panNo=request.getParameter(ApplicationConstant.TRADER_PAN_NO);
		String emailId=request.getParameter(ApplicationConstant.TRADER_EMAIL_ID);
		String desc=request.getParameter(ApplicationConstant.TRADER_DESC);
		
		String landingCost=request.getParameter(ApplicationConstant.LANDING_COST);
		String mulFactor=request.getParameter(ApplicationConstant.MULTIPLY_FACTOR);
		
		String extraField1=request.getParameter(ApplicationConstant.LOSS_QUAN_PER_MT);
		String extrafield2=request.getParameter(ApplicationConstant.EXTRAFIELD2);
				
		String bankName=request.getParameter(ApplicationConstant.BANK_NAME);
		String accountNo=request.getParameter(ApplicationConstant.ACCOUNT_NO);
		String ifscCode=request.getParameter(ApplicationConstant.IFSC_CODE);
		String micrCode=request.getParameter(ApplicationConstant.MICR_CODE);
		
		
		if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.ADD_TRADER)) {
			boolean isAddTrader=false;
			TradersBean traderBean=new TradersBean();
			traderBean.setName(name);
			traderBean.setAddress(address);
			traderBean.setContactNo(contactNo);
			traderBean.setEmailId(emailId);
			traderBean.setDesc(desc);
			traderBean.setMobileNo(mobileNo);
			traderBean.setPanNo(panNo);
			traderBean.setBankName(bankName);
			traderBean.setAccountNo(accountNo);
			traderBean.setMicrCode(micrCode);
			traderBean.setIfscCode(ifscCode);
			traderBean.setExtraField1(extraField1);
			
			traderBean.setMulFactor(mulFactor);
			traderBean.setLandingCost(landingCost);
			
						
			try {
				isAddTrader=traderService.addTraders(traderBean);
				if(isAddTrader){
					message=ApplicationConstant.ADD_TRADER_SUCCESSFULL;
					logger.info("Exception in TradersController AddTraders()--->"+message);
					response.sendRedirect(request.getContextPath()+"/pages/jsp/welcome.jsp?message="+message);
				}
				else{
					message=ApplicationConstant.ADD_TRADER_UNSUCCESSFULL;
					logger.info("Exception in TradersController AddTraders()--->"+message);
					response.sendRedirect(request.getContextPath()+"/pages/jsp/welcome.jsp?message="+message);
				}
			} catch (ApplicationException e) {
				message=ExceptionMessage.UNSUCCESSFULL_ADD_TRADER;
				logger.error("Exception in TradersController AddTraders()--->"+e);
				e.printStackTrace();
			}catch (Exception e) {
				message=ExceptionMessage.UNSUCCESSFULL_ADD_TRADER;
				logger.error("Exception in TradersController AddTraders()--->"+e);
				e.printStackTrace();
			}
			
		
		}else if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.DELETE_TRADER)) {
			boolean isDeleteTrader=false;
			
			try {
				isDeleteTrader=traderService.deleteTraders(companyId);
				if(isDeleteTrader){
					message=ApplicationConstant.DELETE_TRADER_SUCCESSFULL;
					response.sendRedirect(request.getContextPath()+"/tradersInfo?ACTION=TINFO&message="+message);
				}
				else{
					message=ApplicationConstant.DELETE_TRADER_UNSUCCESSFULL;
					response.sendRedirect(request.getContextPath()+"/tradersInfo?ACTION=TINFO&message="+message);
				}
			} catch (ApplicationException e) {
				message=ExceptionMessage.UNSUCCESSFULL_DELETE_TRADER;
				logger.error("Exception in TradersController deleteTraders()--->"+e);
				e.printStackTrace();
			} catch (Exception e) {
				message=ExceptionMessage.UNSUCCESSFULL_DELETE_TRADER;
				logger.error("Exception in TradersController deleteTraders()--->"+e);
				e.printStackTrace();
			}	
		}
		else if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.TRADERS_INFO)) {
			List<Tistit> tradersList=new ArrayList<Tistit>();
			try {
				tradersList=traderService.getTraders();
				
				request.setAttribute("tradersList", tradersList);
				rd = request.getRequestDispatcher("/pages/jsp/tradersInfo.jsp");
				rd.forward(request, response);			
			} catch (ApplicationException e) {
				logger.error("Exception in TradersController getTraders()--->"+e);
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("Exception in TradersController getTraders()--->"+e);
				e.printStackTrace();
			}
		}
		else if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.EDIT_TRADER)) {
			List<Tistit> tradersList=new ArrayList<Tistit>();
			try {
				tradersList=traderService.getTradersById(companyId);
				
				request.setAttribute("traderObj", tradersList);
				rd = request.getRequestDispatcher("/pages/jsp/welcome.jsp");
				rd.forward(request, response);			
			} catch (ApplicationException e) {
				logger.error("Exception in TradersController getTraders()--->"+e);
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("Exception in TradersController getTraders()--->"+e);
				e.printStackTrace();
			}
		} else if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.UPDATE_TRADER)) {
			boolean isUpdateTrader=false;
			TradersBean traderBean=new TradersBean();
			traderBean.setCompanyId(companyId);
			traderBean.setName(name);
			traderBean.setAddress(address);
			traderBean.setContactNo(contactNo);
			traderBean.setEmailId(emailId);
			traderBean.setDesc(desc);
			traderBean.setMobileNo(mobileNo);
			traderBean.setPanNo(panNo);
			
			traderBean.setBankName(bankName);
			traderBean.setAccountNo(accountNo);
			traderBean.setIfscCode(ifscCode);
			traderBean.setMicrCode(micrCode);
			
			traderBean.setMulFactor(mulFactor);
			traderBean.setLandingCost(landingCost);
			
			traderBean.setExtraField1(extraField1);
			try {
				isUpdateTrader=traderService.updateTraders(traderBean);
				if(isUpdateTrader){
					message=ApplicationConstant.UPDATE_TRADER_SUCCESSFULL;
					logger.info("TradersController UpdateTraders()--->"+message);
					response.sendRedirect(request.getContextPath()+"/tradersInfo?ACTION=TINFO");
				}
				else{
					message=ApplicationConstant.UPDATE_TRADER_UNSUCCESSFULL;
					logger.info("Unsuccess in TradersController UpdateTraders()--->"+message);
					rd = request
							.getRequestDispatcher("/pages/jsp/welcome.jsp?message="+message);
					rd.forward(request, response);
				}
			} catch (ApplicationException e) {
				message=ExceptionMessage.UNSUCCESSFULL_UPDATE_TRADER;
				logger.error("Exception in TradersController UpdateTraders()--->"+e);
				e.printStackTrace();
			}catch (Exception e) {
				message=ExceptionMessage.UNSUCCESSFULL_UPDATE_TRADER;
				logger.error("Exception in TradersController UpdateTraders()--->"+e);
				e.printStackTrace();
			}		
		}
	}

}
